﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace taurus.client
{

    /// <summary>
    /// 
    /// </summary>
    public class TArray : ITArray
    {
        private List<TDataWrapper> dataHolder = new List<TDataWrapper>();

        public void add(TDataWrapper wrappedObject)
        {
            dataHolder.Add(wrappedObject);
        }

        public void addBool(bool val)
        {
            addObject(val, TDataType.BOOL);
        }

        public void addByte(byte val)
        {
            addObject(val, TDataType.BYTE);
        }

        public void addByteArray(ByteArray val)
        {
            addObject(val, TDataType.BYTE_ARRAY);
        }

        public void addDouble(double val)
        {
            addObject(val, TDataType.DOUBLE);
        }

        public void addFloat(float val)
        {
            addObject(val, TDataType.FLOAT);
        }

        public void addInt(int val)
        {
            addObject(val, TDataType.INT);
        }

        public void addLong(long val)
        {
            addObject(val, TDataType.LONG);
        }

        public void addNull()
        {
            addObject(null, TDataType.NULL);
        }

        private void addObject(object val, TDataType tp)
        {
            add(new TDataWrapper(tp, val));
        }

        public void addTArray(ITArray val)
        {
            addObject(val, TDataType.TARRAY);
        }

        public void addTObject(ITObject val)
        {
            addObject(val, TDataType.TOBJECT);
        }

        public void addShort(short val)
        {
            addObject(val, TDataType.SHORT);
        }

        public void addString(string val)
        {
            addObject(val, TDataType.STRING);
        }


        private ICollection getArray(int index)
        {
            return getValue<ICollection>(index);
        }

        public bool getBool(int index)
        {
            return getValue<bool>(index);
        }

        public virtual byte getByte(int index)
        {
            return getValue<byte>(index);
        }

        public ByteArray getByteArray(int index)
        {
            return getValue<ByteArray>(index);
        }

        public double getDouble(int index)
        {
            return getValue<double>(index);
        }

        public TDataWrapper get(int index)
        {
            return dataHolder[index];
        }

        public virtual float getFloat(int index)
        {
            return getValue<float>(index);
        }

        public int getInt(int index)
        {
            return getValue<int>(index);
        }


        public long getLong(int index)
        {
            return getValue<long>(index);
        }

        public ITArray getTArray(int index)
        {
            return getValue<ITArray>(index);
        }

        public ITObject getTObject(int index)
        {
            return getValue<ITObject>(index);
        }

        public virtual short getShort(int index)
        {
            return getValue<short>(index);
        }

        public string getString(int index)
        {
            return getValue<string>(index);
        }

        public object getClass(int index)
        {
            return getValue<object>(index);
        }

        public T getValue<T>(int index)
        {
            if (index >= dataHolder.Count)
            {
                return default(T);
            }
            TDataWrapper wrapper = dataHolder[index];
            return (T)wrapper.getObject();
        }

        public void del(int index)
        {
            if (index >= dataHolder.Count)
            {
                return;
            }
            TDataWrapper wrapper = dataHolder[index];
            dataHolder.RemoveAt(index);
        }

        public bool isNull(int index)
        {
            if (index >= dataHolder.Count)
            {
                return true;
            }
            TDataWrapper wrapper = dataHolder[index];
            return (wrapper.getTypeId() == 0);
        }

        public static TArray newFromBinaryData(ByteArray ba)
        {
            return TDataSerializer.me().binary2Array(ba) as TArray;
        }

        public static TArray newFromJson(string json)
        {
            return TDataSerializer.me().json2array(json) as TArray;
        }

        public static TArray newInstance()
        {
            return new TArray();
        }



        public int size()
        {
            return dataHolder.Count;
        }

        public ByteArray toBinary()
        {
            return TDataSerializer.me().array2Binary(this);
        }

        public string toJson()
        {
            return TDataSerializer.me().array2json(this);
        }

        public void addUtfString(string val)
        {
            addString(val);
        }

        public string getUtfString(int index)
        {
            return getString(index);
        }
    }

    /// <summary>
    /// TArrayLite
    /// </summary>
    public class TArrayLite : TArray
    {
        public override byte getByte(int index)
        {

            var num = base.getInt(index);
            bool flag = num < 0;
            if (flag)
            {
                num = 256 + num;
            }
            return Convert.ToByte(num);
        }
        public override short getShort(int index)
        {
            int @int = base.getInt(index);
            return Convert.ToInt16(@int);
        }


        public override float getFloat(int index)
        {
            double @double = base.getDouble(index);
            return Convert.ToSingle(@double);
        }

    }
}

